package org.example;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HtmlUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class NoveCrawler {

    @Test
    public void test01() throws IOException {
        String url = "http://www.biquge.info/10_10582/";
        List<String> catalogs =  parseCatalogs(url);
        catalogs.forEach(c -> System.out.println(c));

    }

    private List<String> parseCatalogs(String url) throws IOException {
        List<String> catalogs = new ArrayList<>();
        Document doc = Jsoup.connect(url).get();
        Elements es = doc.select("#list a");
        es.forEach( e ->catalogs.add(url+e.attr("href")));
        return catalogs;

    }

    @Test
    public void test02() throws IOException {
        String url = "http://www.biquge.info/10_10582/16125732.html";
        Chapter chapter = parseChapter(url);
        System.out.println(chapter.getTitle());
        System.out.println(chapter.getContent());
    }

    private Chapter parseChapter(String url) throws IOException {
        for (int count = 0; count < 5; count++) {
            try {
                Document doc = Jsoup.connect(url).get();
                String title = doc.selectFirst(".bookname>h1").text();
                String content = doc.selectFirst("#content").html();
                //替换掉内容中的标签
                content = HtmlUtil.cleanHtmlTag(content);
                content = content.replaceAll(" ?&nbsp;", " ");
                return new Chapter(title, content);
            } catch (IOException e) {
                System.out.println(e.getMessage());
                System.out.println("Download Error =>"+url);
                System.out.println("等待3秒钟后重试...");
                if(count >= 4) throw  e;
                try {
                    //服务器休息3秒钟，然后再重试！
                    Thread.sleep(3000);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
            }
        }
        return null;
    }

    @Test
    public void test03(){
        start();

    }
    public void start() {
        PrintWriter out =
                FileUtil.getPrintWriter("三寸人间", "UTF-8",true);
        try {
            //1.解析目录
            String url = "http://www.biquge.info/10_10582/";
            List<String> catalogs = parseCatalogs(url);
            //2.遍历目录
            for (String cUrl:catalogs) {
                //3.解析章节
                Chapter chapter = parseChapter(cUrl);
                //4.写出章节
                out.println(chapter.getTitle());
                out.println();
                out.println(chapter.getContent());
                out.println();
                System.out.printf("Download[%s]OK => %s\n",chapter.getTitle(),cUrl);
                //睡眠1秒钟
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }

}

class Chapter{
    private String title;
    private String content;

    public Chapter(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

}